package org.dreamfly.core.util;

import org.dreamfly.core.bean.ResultSetPool;
import org.dreamfly.core.bean.ServerIpInfo;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by IntelliJ IDEA.
 * User: Gin
 * Date: 11-4-12
 * Time: 下午5:01
 * To change this template use File | Settings | File Templates.
 */
public class UrlGetter {
    private ResultSetPool resultSetPool;
    private String applicationName;

    public String getRemoteUrl(final String Url, final String serverName){
        ServerIpInfo ipInfo = resultSetPool.getIpInfo(serverName);
        if(null == ipInfo){
            return null;
        }
        String URL = "/" + applicationName +"/upload/"+Url;
        try {
            URL temp = new URL("http", ipInfo.getIp(), ipInfo.getPort(), URL);
            return ChangeChinese.changeURL(temp).toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * url以http开头返回true
     * @param url
     * @return
     */
    public boolean isNeedTranslate(String url){
        if(null == url){
            return false;
        }else {
            return url.matches("^" + "http" + ".*?");
        }
    }
    public void setResultSetPool(ResultSetPool resultSetPool) {
        this.resultSetPool = resultSetPool;
    }

    public void setApplicationName(String applicationName) {
        this.applicationName = applicationName;
    }
}
